package org.example.all;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;

public class SemaphoreSample {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        for (int i=0;i<5;i++){
            //new Thread(new Task(semaphore,"yangguo+"+i)).start();
            new Thread(new Task(semaphore,"xiaochengzi+"+i)).start();
            //new Thread(new Test("xiaochengzi"+i)).start();
        }
    }

    static class Test extends Thread{
        private String TName;
        public Test(String TName){
            this.TName=TName;
        }

        public  void run(){
            Thread.currentThread().setName(TName);
            System.out.println(Thread.currentThread().getName()+"=========当前线程");
        }
    }

    static class Task extends Thread{
        Semaphore semaphore;

        public Task(Semaphore semaphore,String tname){
            this.semaphore = semaphore;
            super.setName(tname);
        }

        public void run() {
            try {
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName()+":aquire() at time:"+System.currentTimeMillis());

                Thread.sleep(1000);

                semaphore.release();
                //System.out.println(Thread.currentThread().getName()+":aquire() at time:"+System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
